﻿using System.Threading;
using System.Windows.Forms;
using WORD = Microsoft.Office.Interop.Word;
using System.Web;
using System;
using System.IO;
using System.Text;
using System.Configuration;
using System.Collections.Generic;

namespace QldtSdh.Utils
{
    public class WordUtil : IDisposable
    {
        #region Object Const

        object oMissing = System.Reflection.Missing.Value;
        object oVisible = false;
        object oReadOnly = false;
        object oShowConvention = false;
        object oOpenandRepair = true;
        object oTrue = true;
        object oFalse = false;

        #endregion

        #region Fields
        private WORD._Application _wApp = null;
        public WORD._Application WordApplication//
        {
            get
            {
                return _wApp ?? (_wApp = new WORD.Application());
            }
        }
        private WORD.Document _wDoc = null;
        private string fileName;
        #endregion

        #region Constructor
        public WordUtil(string fileName)
        {
            this.fileName = fileName;
        }
        #endregion

        #region public function

        public bool ReplaceText(string findText, string replaceText)
        {
            object obh = WORD.WdDefaultTableBehavior.wdWord9TableBehavior;
            object oaf = WORD.WdAutoFitBehavior.wdAutoFitFixed;
            object omove = WORD.WdMovementType.wdMove;
            object ounitS = WORD.WdUnits.wdScreen;
            object otableBh = WORD.WdDefaultTableBehavior.wdWord9TableBehavior;
            object otableAf = WORD.WdAutoFitBehavior.wdAutoFitFixed;
            object ounitCl = WORD.WdUnits.wdCell;
            object ounitL = WORD.WdUnits.wdLine;
            object ounitC = WORD.WdUnits.wdCharacter;
            object ounitSep = WORD.WdTableFieldSeparator.wdSeparateByTabs;
            object ont = true;
            object ofalse = false;
            object ocount = 3;
            object oreplace;
            oreplace = WORD.WdReplace.wdReplaceAll;
            object ofindtext = findText;
            object orpltext = replaceText;
            WordApplication.Selection.Find.Text = findText;
            WordApplication.Selection.Find.Replacement.Text = replaceText;
            WordApplication.Selection.Find.Forward = true;
            WordApplication.Selection.Find.Wrap = Microsoft.Office.Interop.Word.WdFindWrap.wdFindContinue;
            WordApplication.Selection.Find.MatchCase = false;
            return WordApplication.Selection.Find.Execute(ref ofindtext, ref oTrue, ref oMissing, ref oMissing,
                ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref orpltext,
                ref oreplace, ref oMissing, ref oMissing, ref oMissing, ref oMissing);
        }

        public void PrintDocument(bool deleteFileAfterPrint = false)
        {
            OpenDocument();
            try
            {
                WordApplication.PrintOut();
                Thread.Sleep(4000);
            }
            catch (Exception ex)
            {
            }
            finally
            {
                CloseDocument(false, deleteFileAfterPrint);
            }
        }

        #endregion

        #region private function

        private void OpenDocument(bool visible = false)
        {
            object filen = fileName;
            _wDoc = WordApplication.Documents.Open(ref filen, ref oMissing, ref oMissing, ref oMissing,
                 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing,
                 ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing);

            WordApplication.Visible = visible;
        }

        private WORD.Document CloseDocument(bool saveChanges, bool deleteFileAfterPrint = false)
        {
            try
            {
                object oSave = saveChanges;
                WORD._Document doc = _wDoc;
                doc.Close(ref oSave, ref oMissing, ref oMissing);
                if (deleteFileAfterPrint)
                    File.Delete(fileName);
                return _wDoc;
            }
            catch (Exception ex)
            {
                return null;
            }
        }

        public bool EndWork()
        {
            try
            {
                _wApp.Quit(ref oFalse, ref oMissing, ref oMissing);
                //WordApplication.Quit(ref oFalse, ref oMissing, ref oMissing);
                _wApp = null;
                return true;
            }
            catch (Exception ex)
            {
                _wApp = null;
                return false;
            }
        }

        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            //this.Dispose();
        }

        #endregion
    }
}
